/**
 * 消费习惯管理初始化
 */
var Character = {
    id: "CharacterTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Character.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: '消费习惯', field: 'characters', align: 'center', valign: 'middle', sortable: true},
        {title: '排序',visible: false, field: 'num', align: 'center', valign: 'middle', sortable: true},
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true}];
};

Character.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Character.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加消费习惯
 */
Character.openAddCharacter = function () {
    var index = layer.open({
        type: 2,
        title: '添加消费习惯',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/character/character_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看消费习惯详情
 */
Character.openCharacterDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '消费习惯详情',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/character/character_update/' + Character.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除消费习惯
 */
Character.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/character/delete/" + this.seItem.id, function (data) {
            Feng.success("删除成功!");
            Character.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};


/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Character.formParams = function () {
    var queryData = {};
    queryData['characters'] = $("#character").val();
    return queryData;
}

Character.resetSearch = function () {
    $("#characters").val("");
    Character.search();
};

/**
 * 查询列表
 */
Character.search = function () {
    Character.table.refresh({query: Character.formParams()});
};

$(function () {
    var defaultColunms = Character.initColumn();
    var table = new BSTable(Character.id, "/character/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Character.formParams());
    Character.table = table.init();
    Character.table = table;
});
